package com.bwie.goods.service.impl;

import com.bwie.common.domain.PowerStation;
import com.bwie.common.domain.request.PowerStationRequest;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.goods.mapper.PowerStationMapper;
import com.bwie.goods.service.PowerStationService;
import com.bwie.goods.util.FastUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class PowerStationServiceimpl implements PowerStationService {
    @Autowired
    PowerStationMapper powerStationMapper;

    @Autowired
    FastUtil fastUtil;

    @Override
    public Result<PageResult<PowerStation>> list(PowerStationRequest powerStationRequest) {
        PageHelper.startPage(powerStationRequest.getPageNum(),powerStationRequest.getPageSize());
        List<PowerStation> list=powerStationMapper.list(powerStationRequest);
        PageInfo<PowerStation> powerStationPageInfo = new PageInfo<>(list);
        return PageResult.toResult(powerStationPageInfo.getTotal(),list);
    }

    @Override
    public Result loads(MultipartFile file) {
        String upload="";

        try {
            upload = fastUtil.upload(file);
            if(upload==null){
                return Result.error("上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://1.95.38.198:8888/"+upload,"上传成功");
    }
}
